package com.app.common.utils.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.app.common.utils.bean.BeanUtils; 
import com.app.project.system.user.domain.User;

/**
 * shiro 工具类
 * 
 * @author app
 */
public class ShiroUtils {
	public static String getLoginName() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		String currentPrincipalName = authentication.getName();

		return currentPrincipalName;
	}

	public static void clearCachedAuthorizationInfo() {
		// RealmSecurityManager rsm = (RealmSecurityManager)
		// SecurityUtils.getSecurityManager();
		// UserRealm realm = (UserRealm) rsm.getRealms().iterator().next();
		// realm.clearCachedAuthorizationInfo();
	}

	public static Long getUserId() {
		return 0L;
		// return getUser().getUserId().longValue();
	}

	public static String getIp() {
		return "";// getSubjct().getSession().getHost();
	}

	public static User getUser() {
		User user = new User();
		//BeanUtils.copyBeanProp(user, getSubjct().getPrincipal());
		return user;
	}
	/*
	 * public static Subject getSubjct() { return SecurityUtils.getSubject(); }
	 * 
	 * public static Session getSession() { return
	 * SecurityUtils.getSubject().getSession(); }
	 * 
	 * public static void logout() { getSubjct().logout(); }
	 * 
	 * public static User getUser() { User user = new User();
	 * BeanUtils.copyBeanProp(user, getSubjct().getPrincipal()); return user; }
	 * 
	 * public static void setUser(User user) { Subject subject = getSubjct();
	 * PrincipalCollection principalCollection = subject.getPrincipals(); String
	 * realmName = principalCollection.getRealmNames().iterator().next();
	 * PrincipalCollection newPrincipalCollection = new
	 * SimplePrincipalCollection(user, realmName); // 重新加载Principal
	 * subject.runAs(newPrincipalCollection); }
	 * 
	 * public static void clearCachedAuthorizationInfo() { RealmSecurityManager rsm
	 * = (RealmSecurityManager) SecurityUtils.getSecurityManager(); UserRealm realm
	 * = (UserRealm) rsm.getRealms().iterator().next();
	 * realm.clearCachedAuthorizationInfo(); }
	 * 
	 * public static Long getUserId() { return getUser().getUserId().longValue(); }
	 * 
	 * public static String getLoginName() { return getUser().getLoginName(); }
	 * 
	 * public static String getIp() { return getSubjct().getSession().getHost(); }
	 * 
	 * public static String getSessionId() { return
	 * String.valueOf(getSubjct().getSession().getId()); }
	 */
}
